function findFirstTarget(arr, target) {
  let left = 0;
  let right = arr.length - 1;
  let result = -1;

  while (left <= right) {
      let mid = Math.floor((left + right) / 2);
      
      if (arr[mid] === target) {
          // 找到目标值，但继续向左搜索第一次出现的位置
          result = mid;
          right = mid - 1;
      } else if (arr[mid] < target) {
          left = mid + 1;
      } else {
          right = mid - 1;
      }
  }

  return result;
}

// 测试代码
const arr = [1, 1, 2, 3, 4, 4, 4];
const target = 4;
console.log(findFirstTarget(arr, target)); // 输出: 4